package org.example.dic.core.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.dic.core.entity.DicType;
import org.example.dic.core.form.InsertDicTypeForm;
import org.example.dic.core.form.QueryDicTypeListForm;
import org.example.dic.core.form.UpdateDicTypeForm;
import org.example.dic.core.service.IDicTypeService;
import org.example.dic.core.service.impl.DicCacheService;
import org.example.system.common.utils.ResponseUtil;
import org.example.system.common.vo.PageIntVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 数据字典类型表  前端控制器
 * </p>
 *
 * @author Joey
 * @since 2021-05-13
 */
@ApiSort(1)
@Api(tags = "1、字典管理")
@Slf4j
@RestController
public class DicTypeController {
    @Resource
    IDicTypeService dicTypeService;
    @Resource
    DicCacheService dicCacheService;


    @ApiOperationSupport(order = 201)
    @ApiOperation(value = "DIC201-新增字典类型")
    @PostMapping("/v1/dicType")
    public ResponseUtil insertDicType(InsertDicTypeForm form){
        if(dicTypeService.existByTypeKey(form.getTypeKey(),null)){
            return ResponseUtil.error("类型key已存在");
        }
        DicType dicType = new DicType();
        BeanUtil.copyProperties(dicType,form);
        dicType.setIdPath(dicTypeService.queryIdPath(dicType.getParentId()));
        dicTypeService.save(dicType);
        dicCacheService.dicTypeCacheUpdate(form.getGroupKey());
        return ResponseUtil.success();
    }

    @ApiOperationSupport(order = 202)
    @ApiOperation(value = "DIC202-删除字典类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name="dicTypeId",value="字典类型id",dataType = "long", paramType = "query",required = true)
    })
    @DeleteMapping("/v1/dicType")
    public ResponseUtil delDicType(@NotNull(message = "字典类型id不能为空") Long dicTypeId){
        dicTypeService.deleteByDicTypeId(dicTypeId);
        return ResponseUtil.success();
    }
    @ApiOperationSupport(order = 203)
    @ApiOperation(value = "DIC203-修改字典类型")
    @PutMapping("/v1/dicType")
    public ResponseUtil updateDicType(UpdateDicTypeForm form){
        if(StrUtil.isNotEmpty(form.getTypeKey()) && dicTypeService.existByTypeKey(form.getTypeKey(),form.getId())){
            return ResponseUtil.error("类型key已存在");
        }
        dicTypeService.updateByForm(form);
        return ResponseUtil.success();
    }


    @ApiOperationSupport(order = 299)
    @ApiOperation(value = "DIC299-查询字典类型列表")
    @GetMapping("/v1/dicType/list")
    public ResponseUtil<PageIntVo<DicType>> queryDicTypeList(QueryDicTypeListForm form) {
        List<DicType> list = dicTypeService.queryDicTypeList(form);
        return ResponseUtil.success(PageIntVo.create(list.size(),list));
    }

}

